﻿'
' Will Strohl (will.strohl@gmail.com)
' http://www.willstrohl.com
'
'Copyright (c) 2009-2011, Will Strohl
'All rights reserved.
'
'Redistribution and use in source and binary forms, with or without modification, are 
'permitted provided that the following conditions are met:
'
'Redistributions of source code must retain the above copyright notice, this list of 
'conditions and the following disclaimer.
'
'Redistributions in binary form must reproduce the above copyright notice, this list 
'of conditions and the following disclaimer in the documentation and/or other 
'materials provided with the distribution.
'
'Neither the name of Will Strohl, Lightbox Gallery, nor the names of its contributors may be 
'used to endorse or promote products derived from this software without specific prior 
'written permission.
'
'THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
'EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
'OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
'SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
'INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 
'TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 
'BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
'CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
'ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 
'DAMAGE.
'

Imports DotNetNuke.Security
Imports DotNetNuke.Services.Exceptions
Imports DotNetNuke.UI.Skins.Skin
Imports System.Drawing
Imports System.Web.UI
Imports WillStrohl.Modules.Lightbox.LightboxController

Namespace WillStrohl.Modules.Lightbox

    Partial Public MustInherit Class EditImage
        Inherits WNSPortalModuleBase

#Region " Private Members "

        Private Const IMAGE_FORMAT As String = "<a href=""{0}{1}{2}"" target=""_blank""><img class=""wns-image"" src=""{0}{1}{2}"" alt=""{3}"" title=""{3}""{4} /></a>"

        Private p_ImageId As Integer = Null.NullInteger
        Private p_LightboxId As Integer = Null.NullInteger

#End Region

#Region " Properties "

        Private ReadOnly Property ImageId As Integer
            Get
                If Me.p_ImageId > 0 Then
                    Return Me.p_ImageId
                End If

                Integer.TryParse(Request.QueryString("Image"), Me.p_ImageId)

                Return Me.p_ImageId
            End Get
        End Property

        Private ReadOnly Property LightboxId As Integer
            Get
                If Me.p_LightboxId > 0 Then
                    Return Me.p_LightboxId
                End If

                Integer.TryParse(Request.QueryString("Album"), Me.p_LightboxId)

                Return Me.p_LightboxId
            End Get
        End Property

#End Region

#Region " Event Handlers "

        Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Try

                If Not Me.ImageId > 0 Then
                    SendToModule()
                End If

                If Not Page.IsPostBack Then
                    Me.BindData()
                End If

                Me.Page.Header.Controls.Add(New LiteralControl(String.Format(STYLESHEET_TAG_FORMAT, String.Concat(Me.ControlPath, "js/jquery-ui-1.8.2.css"))))

            Catch exc As Exception ' Module failed to load
                ProcessModuleLoadException(Me, exc, Me.IsEditable)
            End Try
        End Sub

        Private Sub cmdCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdCancel.Click
            SendToModule()
        End Sub

        Private Sub cmdUpdate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdUpdate.Click

            If Me.Page.IsValid Then
                SaveImage()
            End If

        End Sub

#End Region

#Region " Private Helper Functions "

        Private Sub BindData()

            Me.LocalizeModule()

            Dim ctlModule As New LightboxController

            Dim oImage As ImageInfo = ctlModule.GetImageById(Me.ImageId)
            Dim oLightbox As LightboxInfo = ctlModule.GetLightbox(Me.LightboxId)

            If Not oImage Is Nothing Then
                Me.txtImageDescription.Text = oImage.Description
                Me.txtImageTitle.Text = oImage.Title
            End If

            Dim strDimensions As String = String.Empty
            Dim imgFile As Image = Image.FromFile(Server.MapPath(String.Concat(PortalSettings.HomeDirectory, oLightbox.GalleryFolder, oImage.FileName)))

            If imgFile.Width > 500 Then
                strDimensions = GetDimensions(imgFile)
            End If

            Me.litImage.Text = String.Format(IMAGE_FORMAT, PortalSettings.HomeDirectory, oLightbox.GalleryFolder, oImage.FileName, oImage.Title, strDimensions)

            Me.lblFileName.Text = String.Format("{0} &gt; {1}", oLightbox.GalleryName, oImage.FileName)

        End Sub

        Private Function GetDimensions(ByVal oImage As Image) As String

            Dim NewWidth As Integer = 500
            Dim MaxHeight As Integer = 500

            If oImage.Width <= NewWidth Then
                NewWidth = oImage.Width
            End If

            Dim NewHeight As Integer = CType(oImage.Height * NewWidth / oImage.Width, Integer)

            If NewHeight > MaxHeight Then
                NewWidth = CType(oImage.Width * MaxHeight / oImage.Height, Integer)
                NewHeight = MaxHeight
            End If

            Return String.Format(" height=""{0}"" width=""{1}""", NewHeight.ToString, NewWidth.ToString)

        End Function

        Private Sub LocalizeModule()

            Me.cmdCancel.Text = Me.GetLocalizedString("cmdCancel.Text")
            Me.cmdUpdate.Text = Me.GetLocalizedString("cmdUpdate.Text")

            Me.rfvImageDescription.ErrorMessage = Me.GetLocalizedString("rfvImageDescription.ErrorMessage")
            Me.rfvImageTitle.ErrorMessage = Me.GetLocalizedString("rfvImageTitle.ErrorMessage")

        End Sub

        Private Sub SaveImage()

            Dim ctlModule As New LightboxController
            Dim oImage As ImageInfo = ctlModule.GetImageById(Me.ImageId)
            Dim sec As New PortalSecurity

            oImage.Title = sec.InputFilter(Me.txtImageTitle.Text.Trim, PortalSecurity.FilterFlag.NoMarkup)
            oImage.Description = sec.InputFilter(Me.txtImageDescription.Text.Trim, PortalSecurity.FilterFlag.NoScripting)

            ctlModule.UpdateImage(oImage.ImageId, oImage.LightboxId, oImage.FileName, oImage.Title, oImage.Description, oImage.DisplayOrder)

            SendToModule()

        End Sub

        Private Sub SendToModule()

            Response.Redirect(NavigateURL)
            
        End Sub

        Private Sub HandleException(ByVal exc As Exception)
            LogException(exc)
            If Me.UserInfo.IsSuperUser Or Me.UserInfo.UserID = PortalSettings.AdministratorId Then
                AddModuleMessage(Me, _
                    String.Concat(exc.Message, "<br />", exc.StackTrace), _
                    Skins.Controls.ModuleMessage.ModuleMessageType.RedError)
            Else
                AddModuleMessage(Me, exc.Message, _
                    Skins.Controls.ModuleMessage.ModuleMessageType.RedError)
            End If
        End Sub

#End Region

        End Class

End Namespace